<?php

namespace IDock\Insurance\TPBX\Response;

use IDock\Http\Response;
use IDock\Insurance\TPBX\Response\AllTPBXResponse;
use IDock\Printer\EPolicyDownloadPrinter;

class EPolicyDownloadResponse extends AllTPBXResponse
{
    /**
     * @var EPolicyDownloadPrinter $printer
     */
    public $printer = EPolicyDownloadPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody();

        if ($this->printer) {
            $this->printer = new $this->printer;
        }
        $data = $body['data']['basePart'] ?? [];
        $this->printer->finish = true;
        $this->printer->url = $data['printUrl'] ?? '';
        if(empty($this->printer->url)){
            $this->setError('下载电子保单失败');
            $this->printer->doing = true;
            $this->printer->finish = false;
            return $this;
        }
        $this->printer->url = base64_decode($this->printer->url);
        return $this;
    }
}
